<%@page import="com.kaishengit.entity.CarItem"%>
<%@page import="com.kaishengit.dao.ProdDao"%>
<%@page import="com.kaishengit.entity.Prod"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String id = request.getParameter("id");

	try {
		Prod prod = new ProdDao().findById(Integer.parseInt(id));
		
		if(prod == null) {
			response.sendRedirect("home.jsp");
			return;
		}
		
		List<CarItem> shopCar = (List<CarItem>)session.getAttribute("shopCar");
		if(shopCar == null) {
			shopCar = new ArrayList<CarItem>();
			
			CarItem item = new CarItem();
			item.setProd(prod);
			item.setNum(1);
			
			shopCar.add(item);
		} else {
			boolean flag = true;
			for(CarItem item : shopCar) {
				if(item.getProd().getId() == prod.getId()) {
					item.setNum(item.getNum() + 1);
					flag = false;
					break;
				}
			}
			
			if(flag) {
				CarItem item = new CarItem();
				item.setProd(prod);
				item.setNum(1);
				
				shopCar.add(item);
			}
			
		}
		
	
		
		
		session.setAttribute("shopCar", shopCar);
		
		response.sendRedirect("shopcar.jsp");
		
	}catch(NumberFormatException e) {
		response.sendRedirect("home.jsp");
		return;
	}

%>
